package com.phoenix.sgs.faq.general;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.phoenix.sgs.faq.db.DBHelper;
import com.phoenix.sgs.faq.meta.KeyValuePair;
import com.phoenix.sgs.faq.meta.PublishPackage;

public class GeneralHelper {
	public static List<KeyValuePair> getPackages(Context context) {
		SQLiteDatabase db = null;
        Cursor cursor = null;
        List<KeyValuePair> packages = new ArrayList<KeyValuePair>();
        
        try {
			DBHelper dbHelper = new DBHelper(context);
			db = dbHelper.openDatabase();
			cursor = db.rawQuery("SELECT * FROM Packages", null);
			if (cursor == null) {
				return packages;
			}
		    
			int numColumn_id = cursor.getColumnIndex("Id");
			int numColumn_name = cursor.getColumnIndex("Name");
			// int numColumn_offical = cursor.getColumnIndex("IsOfficial");
			
			if (cursor.moveToFirst()) {
				do {
					int id = cursor.getInt(numColumn_id);
					String name = new String(cursor.getBlob(numColumn_name), "GBK").trim();
					// boolean isOfficial = cursor.getInt(numColumn_offical) > 0;
					packages.add(new KeyValuePair(id, name));
				} while (cursor.moveToNext());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			if (db != null) {
				db.close();
			}
		}
		
		return packages;
	}
	
	public static List<KeyValuePair> getCountries(Context context) {
		SQLiteDatabase db = null;
        Cursor cursor = null;
        List<KeyValuePair> items = new ArrayList<KeyValuePair>();
        
        try {
			DBHelper dbHelper = new DBHelper(context);
			db = dbHelper.openDatabase();
			cursor = db.rawQuery("SELECT * FROM Countries", null);
			if (cursor == null) {
				return items;
			}
		    
			int numColumn_id = cursor.getColumnIndex("Id");
			int numColumn_name = cursor.getColumnIndex("Name");
			
			if (cursor.moveToFirst()) {
				do {
					int id = cursor.getInt(numColumn_id);
					String name = new String(cursor.getBlob(numColumn_name), "GBK").trim();
					items.add(new KeyValuePair(id, name));
				} while (cursor.moveToNext());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			if (db != null) {
				db.close();
			}
		}
		
		return items;
	}
	
	public static List<FAQ> getFAQ4General(Context context, int skillId) {
		SQLiteDatabase db = null;
        Cursor cursor = null;
        List<FAQ> items = new ArrayList<FAQ>();
        
        try {
			DBHelper dbHelper = new DBHelper(context);
			db = dbHelper.openDatabase();
			cursor = db.rawQuery("SELECT * FROM FAQ WHERE SkillId = " + skillId, null);
			if (cursor == null) {
				return items;
			}
		    
			int numColumn_id = cursor.getColumnIndex("Id");
			int numColumn_q = cursor.getColumnIndex("Question");
			int numColumn_a = cursor.getColumnIndex("Answer");
			
			if (cursor.moveToFirst()) {
				do {
					int id = cursor.getInt(numColumn_id);
					String q = new String(cursor.getBlob(numColumn_q), "GBK").trim();
					String a = new String(cursor.getBlob(numColumn_a), "GBK").trim();
					items.add(new FAQ(id, q, a));
				} while (cursor.moveToNext());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			if (db != null) {
				db.close();
			}
		}
		
		return items;
	}
}
